﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNShapes;

namespace CNShapeEditor
{
    /// <summary>
    /// This control shows a "session", based on a Session object. This has:
    /// - An shape-editing area for the active page.
    /// - An area showning thumbnails of other pages in the session.
    /// </summary>
    public partial class SessionControl : UserControl
    {
        #region Public properties

        /// <summary>
        /// Property to get or set the session being managed by this control.
        /// </summary>
        public Session Session
        {
            get
            {
                return m_Session;
            }
            set
            {
                // We store the session and register for events from it...
                m_Session = value;
                if (m_Session == null)
                {
                    return;
                }

                m_Session.ActivePageChangedEvent += onActivePageChanged;

                // We pass it to the thumbnail control so that it can show the pages...
                ctrlThumbnails.Session = m_Session;

                // We show the active page...
                ctrlActivePage.Page = m_Session.ActivePage;
            }
        }

        /// <summary>
        /// Property to set the background image of the active page.
        /// </summary>
        public Image ActivePageBackgroundImage
        {
            set
            {
                ctrlActivePage.BackgroundImage = value;
            }
        }

        /// <summary>
        /// Property to set the image shown as the background for the page thumbnails.
        /// </summary>
        public Image ThumbnailBackgroundImage
        {
            set
            {
                ctrlThumbnails.ThumbnailBackgroundImage = value;
            }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public SessionControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Undoes the most recent action on the active page.
        /// </summary>
        public void undo()
        {
            ctrlActivePage.undo();
        }

        /// <summary>
        /// Redoes the most recently undone action on the active page.
        /// </summary>
        public void redo()
        {
            ctrlActivePage.redo();
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Called when the Add Page button is clicked. 
        /// </summary>
        private void cmdAddPage_Click(object sender, EventArgs e)
        {
            m_Session.addPage();
        }

        /// <summary>
        /// Called when the active page in the Session has changed. We show
        /// the new page in the main edit area.
        /// </summary>
        void onActivePageChanged(object sender, Session.Args e)
        {
            ctrlActivePage.Page = m_Session.ActivePage;
            ctrlActivePage.ShapeViewer.sizeToContent();
        }

        #endregion

        #region Private data

        // The session we are showing pictures for...
        private Session m_Session = null;

        #endregion

    }
}
